﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Guan
{
    public class ScrollList : ScrollRect
    {
        float maxDeviationX = 225f;
        Transform group;
        Vector3 comScale0, comScale1, scale1, scale2, scale3;
        Image image0, image1, image2;
        Button button0, button1, button2;
        bool isStart = false;
        bool isLeft = true;
        
        new void Awake()
        {
            this.onValueChanged.AddListener(this.OnMove);
            group = content.GetChild(0);
            scale1 = new Vector3(0.5f, 0.5f, 1);
            comScale0 = new Vector3(0.6f, 0.6f, 1);
            scale2 = new Vector3(0.7f, 0.7f, 1);
            comScale1 = new Vector3(0.8f, 0.8f, 1);
            scale3 = new Vector3(0.9f, 0.9f, 1);
            image0 = group.GetChild(0).GetComponent<Image>();
            image1 = group.GetChild(1).GetComponent<Image>();
            image1 = group.GetChild(2).GetComponent<Image>();
            button0 = group.GetChild(0).GetComponent<Button>();
            button1 = group.GetChild(1).GetComponent<Button>();
            button2 = group.GetChild(2).GetComponent<Button>();
            //button0.onClick.AddListener(test);
            //button1.onClick.AddListener(test);
            //button2.onClick.AddListener(test);
        }
        void test()
        {
            Debug.Log("ONCLICK");
            TurnTableCtrl t = transform.GetComponent<TurnTableCtrl>();
            
        }
        public override void OnBeginDrag(PointerEventData eventData)
        {
            base.OnBeginDrag(eventData);
            isStart = true;
            button0.enabled = false;
            button1.enabled = false;
            button2.enabled = false;
        }
        public override void OnEndDrag(PointerEventData eventData)
        {
            base.OnEndDrag(eventData);
            button0.enabled = true;
            button1.enabled = true;
            button2.enabled = true;
        }
        public override void OnDrag(PointerEventData eventData)
        {
            base.OnDrag(eventData);
        }
        private void OnMove(Vector2 value)
        {
            if (isStart)
                if (value.x < 0) isLeft = true;
                else isLeft = false;
            float lerpValue = Mathf.Abs(value.x) / maxDeviationX;
            //Debug.Log("lerpValue.x:" + lerpValue.ToString());
            Vector3 scale;
            if (isStart)
            {
                if (isLeft)
                {
                    scale = Vector3.Lerp(comScale0, scale2, lerpValue);
                    group.GetChild(0).localScale = scale;
                    scale = Vector3.Lerp(comScale1, scale3, lerpValue);
                    group.GetChild(1).localScale = scale;
                    scale = Vector3.Lerp(comScale0, scale1, lerpValue);
                    group.GetChild(2).localScale = scale;
                }
                else
                {
                    scale = Vector3.Lerp(comScale0, scale1, lerpValue);
                    group.GetChild(0).localScale = scale;
                    scale = Vector3.Lerp(comScale1, scale3, lerpValue);
                    group.GetChild(1).localScale = scale;
                    scale = Vector3.Lerp(comScale0, scale2, lerpValue);
                    group.GetChild(2).localScale = scale;
                }
            }
            else
            {
                if (lerpValue > 1) lerpValue -= 1;
                else lerpValue = 1 - lerpValue;
                if (isLeft)
                {
                    scale = Vector3.Lerp(scale1, comScale0, lerpValue);
                    group.GetChild(0).localScale = scale;
                    scale = Vector3.Lerp(scale2, comScale1, lerpValue);
                    group.GetChild(1).localScale = scale;
                    scale = Vector3.Lerp(scale3, comScale0, lerpValue);
                    group.GetChild(2).localScale = scale;
                }
                else
                {
                    scale = Vector3.Lerp(scale3, comScale0, lerpValue);
                    group.GetChild(0).localScale = scale;
                    scale = Vector3.Lerp(scale2, comScale1, lerpValue);
                    group.GetChild(1).localScale = scale;
                    scale = Vector3.Lerp(scale1, comScale0, lerpValue);
                    group.GetChild(2).localScale = scale;
                }
            }
            //Debug.Log("OnMove---------->value.x:" + value.x.ToString());
            if (Mathf.Abs(value.x) > maxDeviationX)
            {
                Vector3 p = content.localPosition;
                if (value.x > 0)
                {
                    p.x += maxDeviationX * 2;
                    content.localPosition = p;
                    isLeft = false;
                }
                else
                {
                    p.x -= maxDeviationX * 2;
                    content.localPosition = p;
                    isLeft = true;
                }
                if (isStart)
                {
                    isStart = false;
                    MoveChild(group, isLeft);
                }
            }
        }
        void MoveChild(Transform parent, bool toLeft)
        {
            int count = parent.childCount;
            if (toLeft)
            {
                Transform last = parent.GetChild(count - 1);
                last.SetAsFirstSibling();
            }
            else
            {
                Transform fast = parent.GetChild(0);
                fast.SetAsLastSibling();
            }
        }
    }
}